package com.chinamsp.xls;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;

import net.sf.jxls.transformer.XLSTransformer;

/**
 * 扩展 stream 支持 内存导出
 * @author admin
 *
 */
public class ExtendedXlsTransformer extends XLSTransformer{

	/**
	 * 输出xls
	 * @param srcFilePath 模板文件路径
	 * @param beanParams 数据源
	 * @param out 输出流
	 * @throws Exception
	 */
	public void transformXLS(String srcFilePath, Map<String, Object> beanParams, OutputStream out) throws Exception  {
		InputStream is = new BufferedInputStream(new ClassPathResource(srcFilePath).getInputStream());
		// 判断 是否 2007 版本
		boolean is2007 = srcFilePath.toLowerCase().endsWith("xlsx");
		org.apache.poi.ss.usermodel.Workbook workbook = is2007 ?  new XSSFWorkbook(is) :  new HSSFWorkbook(is);
		transformWorkbook(workbook, beanParams);
        OutputStream os = new BufferedOutputStream(out);
        workbook.write(os);
        is.close();
        os.flush();
        os.close();
	}
	
}
